package com.freejava.usejpa.services;

import com.freejava.usejpa.dao.HeroDao;
import com.freejava.usejpa.entity.Hero;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class HeroService {
    @Autowired
    HeroDao heroDao;

    // 添加英雄
    public void addHero(Hero hero) {
        heroDao.save(hero);
    }

    // 分页获取英雄列表
    public Page<Hero> getHerosByPage(Pageable pageable) {
        return heroDao.findAll(pageable);
    }

    // 根据名称查询英雄数据
    public List<Hero> getHerosByNameStartingWith(String name) {
        return heroDao.getHeroByNameContaining(name);
    }

    // 获取最新被创建的英雄
    public Hero getLatestHero() {
        return heroDao.getLatestHero();
    }

}
